//
//  LogonView.swift
//  weibo
//
//  Created by cmh on 15/9/1.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

class LogonView: UIView {
    

    // 添加所有tab试图的登陆页面控件
    func laoutAllUI(imageName: String?, text: String) {
        
        describeLabe.text = text
        
        // 判断是否有图片，有图片就不是首页
        if let iconImage = imageName {
            
//            rotateIconImage.image = UIImage(named: iconImage)
//            
//            // 隐藏小房子
//            homeIconView.hidden = true
            
            homeIconView.image = UIImage(named: iconImage)
            
            rotateIconImage.hidden = true
            
        }else{
        
            setUpRotate()
        }
        
    }
    
    // 旋转图片
    private func setUpRotate() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // 旋转一圈
        animation.toValue = 2 * M_PI
        
        // 旋转时间
        animation.duration = 5
        
        // 旋转次数
        animation.repeatCount = MAXFLOAT
        
        // 不移除动画
        animation.removedOnCompletion = false
        
        // 添加动画
        rotateIconImage.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    // 纯代码开发的时候会被调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        layoutUI()
    }
    
     required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        layoutUI()
    }
    
    // 自动布局添加一个试图的登陆页面控件
    private func layoutUI() {
        
        // 添加控件
        addSubview(rotateIconImage)
        addSubview(coverImageView)
        addSubview(homeIconView)
        addSubview(describeLabe)
        addSubview(registerButton)
        addSubview(logonButton)
        
        // 旋转图片布局
        rotateIconImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: rotateIconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: rotateIconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -80))
        
        // 房子控件布局
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: rotateIconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: rotateIconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 文字控件布局
        describeLabe.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: describeLabe, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: rotateIconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: describeLabe, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: rotateIconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: describeLabe, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 220))
        addConstraint(NSLayoutConstraint(item: describeLabe, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 60))
        
        // 注册控件布局
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: describeLabe, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeLabe, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登陆控件布局
        logonButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: describeLabe, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeLabe, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 遮罩图片布局
        coverImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": coverImageView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-[logonBtn]", options: [], metrics: nil, views: ["v": coverImageView, "logonBtn": logonButton]))
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
    }
    
    // 懒加载房子控件
    private lazy var homeIconView: UIImageView = {
    
        let homeView = UIImageView()
        
        homeView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return homeView
    }()
    
    // 懒加载旋转图片控件
    private lazy var rotateIconImage: UIImageView = {
    
        let rotateView = UIImageView()
        
        rotateView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return rotateView
        
    }()
    
    // 懒加载文字控件
    private lazy var describeLabe: UILabel = {
        
        let describeView = UILabel()
        
        describeView.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        describeView.textAlignment = NSTextAlignment.Center
        describeView.textColor = UIColor.darkGrayColor()
        describeView.font = UIFont.systemFontOfSize(15)
        describeView.numberOfLines = 0
        
        return describeView
        
    }()
    
    // 懒加载注册按钮
    lazy var registerButton: UIButton = {
        
        let registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return registerBtn
        
        }()
    
    // 懒加载登陆按钮
    lazy var logonButton: UIButton = {
        
        let logonBtn = UIButton()
        logonBtn.setTitle("登陆", forState: UIControlState.Normal)
        logonBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        logonBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return logonBtn
        
        
    }()
    
    // 懒加载遮罩控件
    private lazy var coverImageView: UIImageView = {
        
        let coverView = UIImageView()
        
        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return coverView
    }()

}
